package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * servlet 这个包叫做页面控制层
 * 页面所有的数据通过servlet转发到页面
 * service  服务逻辑层
 * 所有逻辑代码都应该出现在service层
 */
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        CompanyService companyService = new CompanyService();
        req.setAttribute("companyModel", companyService.getCompanyModel());

        NavService navService = new NavService();
        List<NavModel> navModelList = navService.getNavModelList();
        req.setAttribute("navModelList", navModelList);

        NewsService newsService = new NewsService();
        List<NewsModel> newsModelList = newsService.getNewsModelList();
        req.setAttribute("newsModelList", newsModelList);

//        ProductModelIO productModel=new ProductModelIO();
        ProductService productService = new ProductService();
        List<ProductModel> productModelList = productService.getProductModelList();
        req.setAttribute("productModelList", productModelList);
//        页面转发
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
    }
/**
 * 1.将html页面修改成jsp页面
 * 2.将top.html用jsp:include引用
 * 3.将值赋值给需要的地方
 *     1.对象  ${companyModel.logo}  companyModel代表后台req.setAttribute("companyModel", companyService.getCompanyModel());
 *     2.集合  代表有无数个对象
 *                 <c:forEach items="${navModelList}" var="model">
 *                 <a href="${model.href}">
 *                     <div>${model.title}</div>
 *                 </a>
 *             </c:forEach>
 *             item对应的时req里面的key值，var代表具体某个对象model.href href代表对象的属性
 *        req.setAttribute("navModelList", navModelList);
 *
 *
 *
 *  后台：
 *  1.新建servlet层，里面要继承public class HomeServlet extends HttpServlet
 *  重写doget方法
 *  2.在类上面加访问后台的地址@WebServlet("/home")
 *  3.在servlet类里面调用服务逻辑层
 *  NavService navService = new NavService();
 *  4.得到具体数据
 *  List<NavModelIO> navModelList = navService.getNavModelList();
 *  5.对作用域request进行赋值
 *  req.setAttribute("navModelList", navModelList);
 *  6.返回当前页面
 *  req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
 *
 *
 *  service层
 *  1.要得到集合数据，新建一个空的集合 List<NavModelIO> list = new ArrayList<>();//集合
 *  2.要得到所有传递给前台的数据
 *  NavModelIO model = new NavModelIO();//只是为了得到数组数据
 *         String hrefs[] = model.getHrefs();//数组
 *         String titles[] = model.getTitles();
 *   3.封装List<NavModelIO>
 *        for (int i = 0; i < hrefs.length; i++) {
 *             NavModelIO navModel = new NavModelIO();//为了封装导航
 *             navModel.setHref(hrefs[i]);
 *             navModel.setTitle(titles[i]);
 *             list.add(navModel);
 *         }
 *   4.返回数据list
 */
}
